//
//  WJItemsControlView.m
//  SliderSegment
//
//  Created by silver on 15/11/3.
//  Copyright (c) 2015年 Fsilver. All rights reserved.
//

#import "WJItemsControlView.h"


@implementation WJItemsConfig

-(id)init
{
    self = [super init];
    if(self){
        
//        _itemWidth = 0;
        _itemFont = [UIFont systemFontOfSize:16];
        _defautItemFont =  [UIFont systemFontOfSize:16];
        _textColor = UIColorFromRGB(0xBBAAAA) ;
        _selectedColor = UIColorFromRGB(0x220000);
        _linePercent = 0.8;
        _lineHieght = 2;
        
    }
    return self;
}

@end


@interface WJItemsControlView(){
    BOOL canScrol;
}

@property(nonatomic,strong)UIView *line;

@property (nonatomic, strong) NSMutableArray *arrayBtn;

@property (nonatomic, strong) UIButton *lastBtn;
@end


@implementation WJItemsControlView

static const NSInteger fitWidth = 20;
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        self.arrayBtn = [NSMutableArray array];
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.scrollsToTop = NO;
        self.tapAnimation = YES;
        
    }
    return self;
}

-(void)setTitleArray:(NSArray *)titleArray
{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    [self.line removeFromSuperview];
    self.line.hidden = YES;
    [self.arrayBtn removeAllObjects];
    _titleArray = titleArray;
    
    if(!_config){
        NSLog(@"请先设置config");
        return;
    }
    
    float x = 0;
    float width = 0;
    float allWidth = 0;
    float height = self.frame.size.height;
    
    for (int i=0; i<titleArray.count; i++) {
        
        NSString *title = titleArray[i];
        UIButton *btn = [[UIButton alloc]init];
        [btn setTitle:title forState:UIControlStateNormal];
        btn.tag = i;
        [btn setTitleColor:_config.textColor forState:UIControlStateNormal];
        btn.titleLabel.font = _config.itemFont;
        [btn addTarget:self action:@selector(itemButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [btn.titleLabel sizeToFit];
        if (_config.itemWidth) {
            width = _config.itemWidth;
        }else{
            width = [btn.titleLabel size].width + 20;
        }
        btn.frame = CGRectMake(x,0,width, height);
        x += width;
        allWidth += width;
        if(i==0){

           [btn setTitleColor:_config.selectedColor forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            
            _currentIndex = 0;
            self.line = [[UIView alloc] initWithFrame:CGRectMake(0,btn.height - 10 , 16, _config.lineHieght)];

            self.line.centerX = btn.centerX;
            if (self.config.lineColor) {
                _line.backgroundColor = self.config.lineColor;
            }else{
//                _line.backgroundColor = HexColor(0xFF3366);
                _line.backgroundColor = UIColorFromRGB(0xFFAA00);
            }
            _line.layer.cornerRadius = _line.height/2.0;
            _line.clipsToBounds = YES;
            [self addSubview:_line];
            self.lastBtn = btn;
        }
        [self.arrayBtn addObject:btn];
    }
    if (allWidth > self.width) {
        canScrol = YES;
    }
//    self.contentSize = CGSizeMake(width, height);
    self.contentSize = CGSizeMake(allWidth, height);
    
}


#pragma mark - 点击事件

-(void)itemButtonClicked:(UIButton*)btn
{
    //接入外部效果
    if (_currentIndex == btn.tag) {
        return;
    }
    
    if(_tapAnimation){
        
        //有动画，由call is scrollView 带动线条，改变颜色
        
        [self changeLine:btn.tag];
    }
    
    [self changeScrollOfSet:btn.tag];
    
    if(self.tapItemWithIndex){
        _tapItemWithIndex(btn.tag,_tapAnimation);
    }
    [self changeItemColor:btn.tag];
    _currentIndex = btn.tag;

}
- (void)changeLine:(NSInteger)index{
    UIButton *btn = [self.arrayBtn objectAtIndex:index];
    __weak __typeof(self)weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.line.centerX = btn.centerX;
        
    }];
}

#pragma mark - Methods

//改变文字焦点
-(void)changeItemColor:(NSInteger)index
{
//    for (int i=0; i<_titleArray.count; i++) {
    if (_currentIndex == index) {
        return;
    }
    UIButton *btn = [self.arrayBtn objectAtIndex:index];
    [btn setTitleColor:_config.selectedColor forState:UIControlStateNormal];
    btn.titleLabel.font = _config.itemFont;
    [_lastBtn setTitleColor:_config.textColor forState:UIControlStateNormal];
    _lastBtn.titleLabel.font = _config.defautItemFont;
    _lastBtn = btn;
    

}

//向上取整
- (NSInteger)changeProgressToInteger:(float)x
{
    
    float max = _titleArray.count;
    float min = 0;
    
    NSInteger index = 0;
    
    if(x< min+0.5){
        
        index = min;
        
    }else if(x >= max-0.5){
        
        index = max;
        
    }else{
        
        index = (x+0.5)/1;
    }
    
    return index;
}


//移动ScrollView
-(void)changeScrollOfSet:(NSInteger)index
{
    if (!canScrol) {
        return;
    }
    float  halfWidth = CGRectGetWidth(self.frame)/2.0;
    float  scrollWidth = self.contentSize.width ;
    UIButton *btn = [self.arrayBtn objectAtIndex:index];
    float leftSpace = btn.width *index - halfWidth + btn.width/2.0;
    
    if(leftSpace<0){
        leftSpace = 0;
    }
    if(leftSpace > scrollWidth- 2*halfWidth){
        leftSpace = scrollWidth-2*halfWidth;
    }
    
    [self setContentOffset:CGPointMake(leftSpace, 0) animated:YES];
}


#pragma mark - 在ScrollViewDelegate中回调
-(void)moveToIndex:(float)x
{
    [self changeLine:x];
    NSInteger tempIndex = [self changeProgressToInteger:x];
    if(tempIndex != _currentIndex){
        //保证在一个item内滑动，只执行一次
        [self changeItemColor:tempIndex];
    }
    _currentIndex = tempIndex;
}

-(void)endMoveToIndex:(float)x
{
    [self changeLine:x];
    [self changeItemColor:x];
    _currentIndex = x;
    
    [self changeScrollOfSet:x];
}





@end




















